import { Public } from '@/decorators/visit.decorator';
import { Body, Controller, Get, Post, Query, Req } from '@nestjs/common';

import {
  ApiBearerAuth,
  ApiOperation,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { Request } from 'express';
import { DynamicDto } from './dto/dynamic.dto';
import { LoveDto } from './dto/love.dto';

import { DynamicService } from './dynamic.service';
import { JwtService } from '@nestjs/jwt';

@ApiTags('动态模块')
@Controller('dynamic')
export class DynamicController {
  constructor(
    private readonly dynamicService: DynamicService,
    private readonly jwtService: JwtService,
  ) {}

  @ApiOperation({ summary: '创建动态' })
  @Post('/create')
  @ApiBearerAuth()
  async create(@Req() req, @Body() dynamicDto: DynamicDto) {
    await this.dynamicService.create({
      ...dynamicDto,
      userId: req.user.id,
    });
    return;
  }

  @Public()
  @ApiOperation({
    summary: '获取动态列表',
  })
  @Get('/getDynamicList')
  @ApiQuery({
    type: Number,
    name: 'lastId',
    description: '首页第一条数据的id',
  })
  @ApiQuery({
    type: Number,
    name: 'size',
    description: '每页数',
  })
  @ApiQuery({
    type: Number,
    name: 'current',
    description: '当前页',
  })
  @ApiQuery({
    type: Number,
    name: 'type',
    description: '动态类型',
  })
  getDynamicList(
    @Query('current') current = 1,
    @Query('size') size = 10,
    @Query('lastId') lastId = 0,
    @Query('type') type = 0,
  ) {
    return this.dynamicService.getDyamicList({
      current: Number(current),
      size: Number(size),
      lastId: Number(lastId),
      type: Number(type),
    });
  }

  @Public()
  @ApiOperation({
    summary: '获取动态详情',
  })
  @Get('/getDynamicDetail')
  @ApiQuery({
    name: 'id',
    description: '动态id',
  })
  @ApiBearerAuth()
  getDynamicDetail(@Query('id') id: number, @Req() req: Request) {
    let userId = 0;
    try {
      const result = this.jwtService.decode(
        String(req.headers.authorization).replace('Bearer ', ''),
      );
      if (result) {
        console.log(result);
        if ((result as Record<string, any>)?.id) {
          userId = Number((result as Record<string, any>).id);
        }
      }
    } catch (error) {
      console.log('error', error);
    }
    return this.dynamicService.getDynamicDetail(Number(id), Number(userId));
  }

  @ApiOperation({
    summary: '点赞/取消点赞',
  })
  @Post('toLove')
  @ApiBearerAuth()
  toLove(@Req() req, @Body() loveDto: LoveDto) {
    loveDto.likeUserId = req.user.id;
    return this.dynamicService.toLove(loveDto);
  }

  @Public()
  @ApiOperation({
    summary: '获取个人所有动态（分页）',
  })
  @Get('/getPersonalDynamicList')
  getPersonalDynamicList(
    @Query('current') current = 1,
    @Query('size') size = 10,
    @Query('lastId') lastId = 0,
    @Query('userId') userId = 0,
    @Query('type') type = 0,
  ) {
    return this.dynamicService.getPersonalDynamicList(
      {
        current: Number(current),
        size: Number(size),
        lastId: Number(lastId),
        type: Number(type),
      },
      Number(userId),
    );
  }
}
